﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using Common;
using Newtonsoft.Json;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {

        
        
            //获取当前联系人列表
            Process();
        }


        private async static void Process()
        {
            //post
            var request = new RequestContext<Contact>();
            request.Uri = new Uri("http://127.0.0.1:8010/api/contacts");
            Contact contact = new Contact { Name = "王五", PhoneNo = "0512-34567890", EmailAddress = "wangwu@gmail.com" };
            request.Data = contact;
            ApiProxy.GetInstance().PostResult(request);
   
            Console.WriteLine("添加新联系人“王五”：");
 
      
            Console.WriteLine("当前联系人列表：");
            //ListContacts(contacts.DatList);


            //put
            request.Uri = new Uri("http://127.0.0.1:8010/api/contacts/1");
            
            request.Data = contact;
            Contact con =ApiProxy.GetInstance().GetResult(request).DatList.FirstOrDefault();
            con.EmailAddress = "00006";
            request.Data = con;
            ApiProxy.GetInstance().PutResult(request);

              
            //delete
            request.Uri = new Uri("http://127.0.0.1:8010/api/contacts/4");
           Console.WriteLine( ApiProxy.GetInstance().DeleteResult(request).Code);

            //Get
            request.Uri = new Uri("http://127.0.0.1:8010/api/contacts");

             var  contacts = ApiProxy.GetInstance().GetResult(request);

            ListContacts(contacts.DatList);


        }

        private static void ListContacts(IEnumerable<Contact> contacts)
        {
            foreach (Contact contact in contacts)
            {
                Console.WriteLine("{0,-6}{1,-6}{2,-20}{3,-10}", contact.Id, contact.Name, contact.EmailAddress,
                    contact.PhoneNo);
            }
            Console.ReadKey();
        }






    }
}
